//设置上传图片模块的id
$("input[name='id']").val(getQueryString("id"));

//打开上传图片的modal
function openUploadModal(targ) {
    $("#img-new-modal").modal();
    $("#img-new-modal").find(".btn-primary").attr("onclick","uploadGallery("+targ+")");
}

//获取reviews
getReviews(0);
//获取gallery
getGallery();

//在加载完以后切换成同步
$.ajaxSetup({
    async: false
});

//获取个人信息
getProfileMessage(1);

//编辑review
function editorReview() {
    $(this).unbind("click");
    var parent=$(this).parents(".introduce");

    var title=parent.find(".review-title");
    //对原来的文本进行替换，先清空原有的\n再把空行填补回去

    //对areText模块进行加工
    var editTitleNode=reviewEditNode(title,"title");
    editTitleNode.find(".text-right").remove();

    var content=parent.find(".review-content");
    var editContentNode=reviewEditNode(content,"content");


    //绑定取消事件
    editContentNode.find(".glyphicon-remove").parent(".btn-default").click(reviewCancelEdit);

    //绑定确认提交事件
    editContentNode.find(".glyphicon-ok").parent(".btn-default").click(reviewSubmitEdit);

    title.after(editTitleNode);
    content.after(editContentNode);
    title.hide();
    content.hide();
}
//获取加工后的editNode
function reviewEditNode(parent,name) {
    var msg=parent.html().replace(new RegExp("\n","g"),"").replace(new RegExp("<br>","g"),"\n");
    var node=$(textAre(null,parent.attr("class")));
    node.find("textarea").text(msg);
    node.find("textarea").attr("name",name);

    return node;
}

//review取消编辑事件
function reviewCancelEdit(parent) {
    var editNode;
    if(parent[0]==undefined){
        editNode=$(this).parents(".review").find(".introduce-edit");
        parent=$(this).parents(".review");
    }else {
        editNode=parent.find(".introduce-edit");
    }

    //重新绑定修改事件
    parent.find(".btn-default").click(editorReview);

    //处理editNode
    editNode.prev().show();
    editNode.remove();
}

//review提交事件
function reviewSubmitEdit() {
    var parent=$(this).parents(".review");
    var title=parent.find("textarea[name='title']").val();
    var content=parent.find("textarea[name='content']").val();
    var rid=parent.attr("rid");
    var id=getQueryString("id");

    var json={id:id,rid:rid,title:title,content:content};

    console.log(json);

    $.get("/personage/UpdataReview",json,function (data) {
        if(data.code==0){
            alert(data.msg);
        }else {
            //如果操作成功则改写原来的内容
            parent.find(".review-title").html(title);
            parent.find(".review-content").html(content.replace(new RegExp("\n","g")));

            reviewCancelEdit(parent);
        }
    })
}

//新增回顾review
function newReview() {
    var reviews=$("#review").find(".review");
    if(reviews.length>=10){
        alert("回顾已满十篇，不能继续增加了。");
    }

    if(confirm("你确定要增加一篇回顾吗?")){
        $.get("/personage/insertReview",{id:getQueryString("id"),title:"",content:""},function (data) {
            if(data.code==0){
                alert(data.msg);
            }else {
                $("#review .review").remove();
                getReviews(0);
            }
        })
    }
}

//删除review
function removeReview() {
    var parent=$(this).parents(".review");

    if(confirm("你确定要删除这篇回顾吗，删除后不可恢复！")){
        var rid=$(this).parents(".review").attr("rid");
        $.get("/personage/deleteReview",{id:getQueryString("id"),rid:rid},function (data) {
            if(data.code==0){
                alert(data.msg);
            }else {
                parent.remove();
            }
        })
    }
}

//上传gallery
function uploadGallery(targ) {
    if($("#gallery .img-picture img").length>=10){
        alert("图片已满十张，不能继续上传");
        return;
    }

    var formData=new FormData($("#form-gallery")[0]);

    if(targ==1){
        url="/personage/uploadImg";
    }else if(targ==2){
        url="/personage/uploadGallery";
    }

    $.ajax({
        cache: true,
        type: "POST",
        url:url,
        data:formData,
        processData: false,
        contentType: false,
        async: false,
        error: function(data) {
            alert("Connection error");
        },
        success: function(data) {
            if(data.code==0){
                alert(data.msg);
            }else {
                if(targ==2){
                    getGallery();
                }else if(targ==1){
                    location.reload();
                }
            }

            $("#img-new-modal").modal("hide");
        }
    })
}

//删除gallery
function removeGallery() {
    var img=$("#modal-img").find(".img-thumbnail");
    var pid=img.attr("pid");
    if(confirm("你确定要删除这样照片吗，删除后不可恢复！")){
        $.get("/personage/removeGallery",{pid:pid,id:getQueryString("id")},function (data) {
            if(data.code==0){
                alert(data.msg);
            }else {
                $(".img-picture").find("img[pid='"+pid+"']").remove();
            }

            $("#modal-img").modal("hide");
        });
    }
}

//绑定修改事件
$(".introduce").find(".btn-default").click(editorMessage);